package org.zoomdev.zoom.dao.adapters.parameter;

import org.zoomdev.zoom.dao.adapters.ParameterAdapter;

import java.sql.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;

// 集合类型适配器（转换为数组）
public class CollectionParameterAdapter<T> implements ParameterAdapter<Collection<T>> {
    private final String elementTypeName;

    public CollectionParameterAdapter(String elementTypeName) {
        this.elementTypeName = elementTypeName;
    }


    @Override
    public void setNonnullParameter(PreparedStatement ps, int index, Collection<T> data) throws SQLException {
        Connection conn = ps.getConnection();
        Object[] array = data.toArray();
        Array sqlArray = conn.createArrayOf(elementTypeName, array);
        ps.setArray(index, sqlArray);
    }
}
